package com.mars.module.admin.controller;


import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import com.mars.common.enums.BusinessType;
import com.mars.common.response.sys.NewsCategoryResponse;
import com.mars.common.response.sys.SysArticleCategoryResponse;
import com.mars.common.result.R;
import com.mars.common.util.ExcelUtils;
import com.mars.module.admin.request.NewsCategoryRequest;
import io.swagger.annotations.Api;
import com.mars.framework.annotation.RateLimiter;
import com.mars.module.admin.entity.SysArticle;
import com.mars.framework.annotation.Log;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import com.mars.common.response.PageInfo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.service.ISysArticleService;
import com.mars.module.admin.request.SysArticleRequest;

import javax.servlet.http.HttpServletResponse;

/**
 * 文章控制层
 *
 * @author mars
 * @date 2024-03-18
 */
@Slf4j
@AllArgsConstructor
@RestController
@Api(value = "1.文章接口管理",tags = "1.文章接口管理")
@RequestMapping("/admin/sysArticle" )
public class SysArticleController {

    private final ISysArticleService iSysArticleService;

    /**
     * 分页查询文章列表
     */
    @ApiOperation(value = "分页查询文章列表")
    @PostMapping("/pageList")
    public R<PageInfo<SysArticle>> pageList(@RequestBody SysArticleRequest sysArticleRequest) {
        return R.success(iSysArticleService.pageList(sysArticleRequest));
    }


    /**
     * 获取文章详细信息
     */
    @ApiOperation(value = "获取文章详细信息")
    @GetMapping(value = "/query/{id}")
    public R<SysArticle> detail(@PathVariable("id") Long id) {
        return R.success(iSysArticleService.getById(id));
    }

    /**
     * 新增文章
     */
    @Log(title = "新增文章", businessType = BusinessType.INSERT)
    @RateLimiter
    @ApiOperation(value = "新增文章")
    @PostMapping("/add")
    public R<Void> add(@RequestBody SysArticleRequest sysArticleRequest) {
        iSysArticleService.add(sysArticleRequest);
        return R.success();
    }

    /**
     * 修改文章
     */
    @Log(title = "修改文章", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改文章")
    @PostMapping("/update")
    public R<Void> edit(@RequestBody SysArticleRequest sysArticleRequest) {
        iSysArticleService.update(sysArticleRequest);
        return R.success();
    }

    /**
     * 删除文章
     */
    @Log(title = "删除文章", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除文章")
    @PostMapping("/delete/{ids}")
    public R<Void> remove(@PathVariable Long[] ids) {
        iSysArticleService.deleteBatch(Arrays.asList(ids));
        return R.success();
    }

    /**
    * 导出文章
    *
    * @param response response
    * @throws IOException IOException
    */
    @PostMapping(value = "/export")
    public void exportExcel(HttpServletResponse response,@RequestBody SysArticleRequest sysArticleRequest) throws IOException {
        List<SysArticle> list = iSysArticleService.list(sysArticleRequest);
        ExcelUtils.exportExcel(list, SysArticle.class, "文章信息", response);
    }
}
